const fs = require('fs');
const path = require('path');

let oldHtmlPath = path.join(__dirname, 'index.html');
let newHtmlPath = path.join(__dirname, 'abc', 'index.html');
let cssPath = path.join(__dirname, 'abc', 'index.css');
let jsPath = path.join(__dirname, 'abc', 'index.js');

fs.readFile(oldHtmlPath, 'utf-8', (err, data) => {
    if (err) return console.log(err);
    // 提取css代码
    let cssReg = /<style>([\s\S]*)<\/style>/g;
    let cssRet = cssReg.exec(data);
    fs.writeFile(cssPath, cssRet[1], err => {
            if (err) return console.log(err);
            console.log('css分离成功');
        })
        // 提取js代码
    let jsReg = /<script>([\s\S]*)<\/script>/g;
    let jsRet = jsReg.exec(data);
    fs.writeFile(jsPath, jsRet[1], (err) => {
            if (err) return console.log(err);
            console.log('js分离成功', );
        })
        // 拆index.html
    data = data.replace(cssReg, '<link rel="stylesheet" href="index.css">');
    data = data.replace(jsReg, '<script src="index.js"></script>');
    fs.writeFile(newHtmlPath, data, err => {
        if (err) {
            return console.log(err)
        }

        console.log('拆分成功')
    })
})